#include "JMainWindow.h"

#include <QtGui/QApplication>
#include <QtOpenGL>
#include <QSettings>

#include "JLog.h"
#include "PythonQt.h"

int main(int argc, char *argv[])
{
	Log.ClearLog("JointMD.log");

	QApplication a(argc, argv);

	// Define Organization name, domain and application name
	QCoreApplication::setOrganizationName("OBLab");
	QCoreApplication::setOrganizationDomain("OBLab.com");
	QCoreApplication::setApplicationName("JointMD");

	QSettings settings;
	
	// The splash screen
	QPixmap pixmap(":/JMainWindow/Resources/splash.png");
	QSplashScreen splash;
	splash.setPixmap(pixmap);
	splash.setMask(pixmap.mask());
	splash.show();
	Sleep(100);

	// Test if the system has OpenGL Support
	if (!QGLFormat::hasOpenGL())
	{
		QString message(QObject::tr("This System has no OpenGL support"));
		Log.Post(message,JLOG_CRITICAL);
		QMessageBox::critical(NULL, QCoreApplication::applicationName(), message);
		return 1;
	}
	Log.PostSub("OpenGL Support Found", JLOG_SUCCESS);

	splash.showMessage("OpenGL Found!", Qt::AlignVCenter|Qt::AlignHCenter|Qt::AlignJustify, Qt::red);
	a.processEvents();
	Sleep(500);

	// Test if the system support frame buffer Object
	const bool frameBufferIsSupported = QGLFramebufferObject::hasOpenGLFramebufferObjects();
	if (!frameBufferIsSupported) Log.PostSub("No FrameBuffer Support Found",JLOG_WARNING);
	Log.PostSub("OpenGL FrameBuffer Support Found", JLOG_SUCCESS);

	splash.showMessage("OpenGL FrameBuffer Supported!", Qt::AlignVCenter|Qt::AlignHCenter|Qt::AlignJustify, Qt::red);
	a.processEvents();
	Sleep(500);

	// Init Python Scripting
	PythonQt::init();
	Log.PostSub("PythonQt Script Engine Initialized");

	splash.showMessage("Python Engine Started...", Qt::AlignVCenter|Qt::AlignHCenter|Qt::AlignJustify, Qt::red);
	a.processEvents();
	Sleep(500);

	splash.showMessage("Launching...", Qt::AlignVCenter|Qt::AlignHCenter|Qt::AlignJustify, Qt::red);
	a.processEvents();
	Sleep(500);

	JMainWindow w;
	w.show();

	splash.finish(&w);

	return a.exec();
}
